package com.example.dog.core.config;

import com.example.dog.core.entity.Dog;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean("userRedisTemplate")
    public RedisTemplate<String, Dog> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Dog> template = new RedisTemplate<String,Dog>();
        Jackson2JsonRedisSerializer<Dog> j = new Jackson2JsonRedisSerializer<Dog>(Dog.class);
        template.setValueSerializer(j);
        template.setHashValueSerializer(j);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}